

jsal.module({
    name : 'heist.tests.runtime',
    depends : ['heist.runtime'],
    onload : function () {
        if (! heist.tests) heist.tests = {};
        heist.tests.runtime = new testjs.Test(
            'runtime', function () {
		        this.plan(14);

				this.ok(heist.runtime, 'runtime is loaded');

				this.throwsOk(
                    function () {
					    new heist.runtime.Handler({})
				    },
                    /roots is a required option/,
				    'exception thrown when roots not passed'
				);

				var handler = new heist.runtime.Handler({
					roots : 'comps'
				});

				this.isa(handler, heist.runtime.Handler, 'create a handler');

				this.is(handler.roots.length, 1, 'handler has one root');

				this.is(handler.roots[0].path, 'comps', 'root has correct path');

				handler = new heist.runtime.Handler({
					roots : ['comps', 'comps2']
				});

				this.isa(handler, heist.runtime.Handler, 'create a handler with multiple roots');

				this.is(handler.roots.length, 2, 'handler has two roots');

				this.is(handler.roots[0].path, 'comps', 'first root has correct path');
				this.is(handler.roots[1].path, 'comps2', 'second root has correct path');

 				var test = this;

				// test loading a compiled component that exists when set to fallback
				this.async('load compiled', function (handle) {
					handler.load('/test/complc', true, function () {
						this.is(handler.comp('/test/complc'), 'hello from inside loaded comp');
						handle.finish();
					}, this);
				});

				return;

				// test loading a compiled component that does not exist when set to fallback
				// i.e. actually fallback to source
				this.async('fallback for load compiled', function (handle) {
					handler.load('/test/compflc', true, function () {
						this.is(handler.comp('/test/compflc'), 'hello from inside loaded fallback comp');
						handle.finish();
					}, this);
				});

				// test loading a source component
				this.async('load source', function (handle) {
					handler.load('/test/compsrc', null, function () {
					    this.is(handler.comp('/test/compsrc'), 'hello from inside source comp');
						handle.finish();
					}, this);
				});

				// test loading a compiled component that exists when configured not to load source as fallback
				this.async('load compiled without fallback', function (handle) {
					handler.load('/test/complc', false, function () {
						this.is(handler.comp('/test/complc'), 'hello from inside loaded comp');
						handle.finish();
					}, this);
				});

				// test load a compiled component that does not exist whe configured not to load source as fallback
				this.async('load non-existant compiled without fallback', function (handle) {
					handler.load('/test/compflc', false, function () {
						this.is(handler.comp('/test/compflc'), 'hello from inside loaded fallback comp');
						handle.finish();
					}, this);
				});
			 }
		);
	}
});

